package cn.toolck.minitomcat.start;

import cn.toolck.minitomcat.pojo.Request;
import cn.toolck.minitomcat.pojo.Response;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/*
 * 需求进行http请求并相应一个静态资源
 * */
public class Starter_0_2 extends AbstractStarter implements Starter {

    @Override
    public void init() {

    }

    @Override
    public void start() {
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(8082);
        } catch (
                Exception e) {
            e.printStackTrace();
        }
        while (true) {
            try {
                final Socket socket = serverSocket.accept();
                InputStream in = socket.getInputStream();
                OutputStream out = socket.getOutputStream();
                Request request = new Request(in);
                System.out.println(request.getUrl());
                Response response = new Response(out);
                response.outputHtml(request.getUrl());
                out.flush();
                //关闭
                out.close();
                in.close();
                socket.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
